﻿@{
    ViewData["Title"] = "首页";
}

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
        <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <img src="~/images/banner1.svg" alt="ASP.NET" class="img-responsive" />
            <div class="carousel-caption" role="option">
                <p>
                    Learn how to build ASP.NET apps that can run anywhere.
                    <a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409">
                        Learn More
                    </a>
                </p>
            </div>
        </div>
        <div class="item">
            <img src="~/images/banner2.svg" alt="Visual Studio" class="img-responsive" />
            <div class="carousel-caption" role="option">
                <p>
                    There are powerful new features in Visual Studio for building modern web apps.
                    <a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525030&clcid=0x409">
                        Learn More
                    </a>
                </p>
            </div>
        </div>
        <div class="item">
            <img src="~/images/banner3.svg" alt="Package Management" class="img-responsive" />
            <div class="carousel-caption" role="option">
                <p>
                    Bring in libraries from NuGet, Bower, and npm, and automate tasks using Grunt or Gulp.
                    <a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525029&clcid=0x409">
                        Learn More
                    </a>
                </p>
            </div>
        </div>
        <div class="item">
            <img src="~/images/banner4.svg" alt="Microsoft Azure" class="img-responsive" />
            <div class="carousel-caption" role="option">
                <p>
                    Learn how Microsoft's Azure cloud platform allows you to build, deploy, and scale web apps.
                    <a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525027&clcid=0x409">
                        Learn More
                    </a>
                </p>
            </div>
        </div>
    </div>
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>
<div>
    <h4>项目结构及依懒项</h4>
    <ul>
        <li>Wunion.DataAdapter.NetCore 核心库（无依懒项）</li>
        <li>Wunion.DataAdapter.NetCore.SQLite3 数据库支持（依懒NuGet包：Microsoft.Data.Sqlite）</li>
        <li>Wunion.DataAdapter.NetCore.SQLServer 数据库支持（依懒NuGet包：System.Data.SqlClient）</li>
    </ul>
</div>
<br />
<div class="panel code-panel">
    <h4>引用及初始化组件库</h4>
    在您的项目中必须引用库 Wunion.DataAdapter.NetCore ,然后根据数据库的选用情况分别引用 Wunion.DataAdapter.NetCore.SQLServer 或 Wunion.DataAdapter.NetCore.SQLite3 支持库（目前只有这两种数据库的支持）。
    <p></p>
    <p>
        <strong>命名空间引用：</strong><br>
        <span class="format-keywords">using</span> Wunion.DataAdapter.Kernel; <br />
        <span class="format-keywords">using</span> Wunion.DataAdapter.Kernel.SQLite3; 或 Wunion.DataAdapter.Kernel.SQLServer; <br />
        <span class="format-keywords">using</span> Wunion.DataAdapter.Kernel.SQLite3.CommandParser; 或 Wunion.DataAdapter.Kernel.SQLServer.CommandParser; <br />
    </p>
    <p>
        <strong>数据引擎初始化</strong><br />
        <span class="format-class">SqlServerDbAccess</span> MsSqlDBA = <span class="format-keywords">new</span> <span class="format-class">SqlServerDbAccess()</span>;<br />
        MsSqlDBA.ConnectionString = <span class="format-strVal">"Server=(local);Database=Wunion.DataAdapter.NetCore.Demo;User ID=sa;Password=XXX;Trusted_Connection=True;"</span>;<br />
        <span class="format-class">DataEngine</span>.AppendDataEngine(MsSqlDBA, <span class="format-keywords">new</span> <span class="format-class">SqlServerParserAdapter()</span>);&nbsp;&nbsp;<span class="format-note">// 添加为默认引擎</span><br />
        <span class="format-note">// 追加多个数据引擎实例↓</span><br />
        <span class="format-class">SqliteDbAccess</span> SqliteDBA = <span class="format-keywords">new</span> <span class="format-class">SqliteDbAccess()</span>;<br />
        SqliteDBA.ConnectionString = <span class="format-strVal">"Data Source={wwwroot}Test.d"</span>;<br />
        <span class="format-class">DataEngine</span>.AppendDataEngine(SqliteDBA, <span class="format-keywords">new</span> <span class="format-class">SqliteParserAdapter()</span>, <span class="format-strVal">"SQLite"</span>);
    </p>
    <p>
        注：ASP.NET Core 项目通常应在 Startup 类的 Configure 方法中使用以上代码初始化组件库。
    </p>
</div>
<div class="panel code-panel">
    <h4>数据查询语法</h4>
    <p>
        <strong>查询单表</strong><br />
        <span class="format-class">DbCommandBuilder</span> Command = <span class="format-keywords">new</span> <span class="format-class">DbCommandBuilder()</span>;<br />
        Command.Select(<span class="format-class">td</span>.Field(<span class="format-strVal">"Field1"</span>), <span class="format-class">td</span>.Field(<span class="format-strVal">"Field2"</span>))<br />
        <span class="code-indent-1">.From</span>(<span class="format-strVal">"TableName"</span>)<br />
        <span class="code-indent-1">.Where</span>(<span class="format-class">td</span>.Field(<span class="format-strVal">"Field3"</span>) == <span class="format-keywords">true</span>);<br />
        <span class="format-class">SpeedDataTable</span> dt = <span class="format-class">DataEngine</span>.CurrentEngine.ExecuteQuery(Command);<br />
        <span class="format-note">//  查询所有字段请使用：td.Field("*")</span><br />
        <br />
        <strong>多表查询(LEFT JOIN)</strong><br />
        <span class="format-class">DbCommandBuilder</span> Command = <span class="format-keywords">new</span> <span class="format-class">DbCommandBuilder()</span>;<br />
        Command.Select(<span class="format-class">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"TestId"</span>),<span class="format-note">  // 查询其中某个表的所字段可用：td.Field("t2", "*")</span><br />
        <span class="format-class indent-select-td">td</span>.Field(<span class="format-strVal">"t1"</span>, <span class="format-strVal">"GroupName"</span>),<br />
        <span class="format-class indent-select-td">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"TestName"</span>),<br />
        <span class="format-class indent-select-td">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"TestAge"</span>),<br />
        <span class="format-class indent-select-td">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"TestSex"</span>))<br />
        <span class="code-indent-1">.From</span>(<span class="format-class">fm</span>.Table(<span class="format-strVal">"DATA_TABLE"</span>, <span class="format-strVal">"t2"</span>),<br />
        <span class="format-class indent-leftjion">fm</span>.LeftJoin(<span class="format-strVal">"GROUP_TABLE"</span>, <span class="format-strVal">"t1"</span>)<br />
        <span class="indent-leftjion-on">.ON</span>(<span class="format-class">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"GroupId"</span>) == <span class="format-class">td</span>.Field(<span class="format-strVal">"t1"</span>, <span class="format-strVal">"GroupId"</span>))<br />
        <span class="code-indent-1">)</span><br />
        <span class="code-indent-1">.Where(</span><span class="format-class">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"GroupId"</span>) == groupId)<br />
        <span class="code-indent-1">.Paging</span>(pageSize, currentPage, <span class="format-class">td</span>.Field(<span class="format-strVal">"t2"</span>, <span class="format-strVal">"TestId"</span>), <span class="format-interface">OrderByMode</span>.DESC);<span class="format-note">  // 使用分页功能</span><br />
        <span class="format-class">SpeedDataTable</span> dt = <span class="format-class">DataEngine</span>.CurrentEngine.ExecuteQuery(Command);<br />
    </p>
</div>
<div class="panel code-panel">
    <h4>表的更新、插入及删除</h4>
    <p>
        <strong>插入数据</strong><br />
        <span class="format-class">DbCommandBuilder</span> Command = <span class="format-keywords">new</span> <span class="format-class">DbCommandBuilder()</span>;<br />
        Command.Insert(<span class="format-strVal">"DATA_TABLE"</span>, <span class="format-class">td</span>.Field(<span class="format-strVal">"GroupId"</span>), <span class="format-class">td</span>.Field(<span class="format-strVal">"TestName"</span>), <span class="format-class">td</span>.Field(<span class="format-strVal">"TestAge"</span>))<br />
        <span class="code-indent-1">.Values</span>(<span class="format-interface">2</span>, <span class="format-strVal">"The test name."</span>, <span class="format-interface">12.3f</span>);<br />
        <span class="format-keywords">int</span> count = <span class="format-class">DataEngine</span>.CurrentEngine.ExecuteNoneQuery(Command);<br />
        <span class="format-keywords">int</span> TestId = <span class="format-class">Convert</span>.ToInt32(<span class="format-class">DataEngine</span>.CurrentEngine.DBA.SCOPE_IDENTITY);<span class="format-note"> // 获取插入后自增量字段的值。</span>
    </p><br />    
    <p>
        <strong>更新表</strong><br />
        <span class="format-class">DbCommandBuilder</span> Command = <span class="format-keywords">new</span> <span class="format-class">DbCommandBuilder()</span>;<br />
        Command.Update(<span class="format-strVal">"DATA_TABLE"</span>)<br />
        <span class="code-indent-1">.Set</span>(<span class="format-class">td</span>.Field(<span class="format-strVal">"TestName"</span>) == NewName, <span class="format-class">td</span>.Field(<span class="format-strVal">"TestAge"</span>) == Age)<br />
        <span class="code-indent-1">.Where</span>(<span class="format-class">td</span>.Field(<span class="format-strVal">"TestId"</span>) == Id);<br />
        <span class="format-keywords">int</span> result = <span class="format-class">DataEngine</span>.CurrentEngine.ExecuteNoneQuery(Command);
    </p><br />
    <p>
        <strong>删除表数据(不支持多表删除)</strong><br />
        <span class="format-class">DbCommandBuilder</span> Command = <span class="format-keywords">new</span> <span class="format-class">DbCommandBuilder()</span>;<br />
        Command.Delete(<span class="format-strVal">"DATA_TABLE"</span>)<br />
        <span class="code-indent-1">.Where</span>(<span class="format-class">td</span>.Field(<span class="format-strVal">"TestId"</span>) == Id,<br />
        <span class="indent-where format-class">exp</span>.And,<br />
        <span class="indent-where format-class">td</span>.Field(<span class="format-strVal">"GroupId"</span>) == GrpId);<br />
        <span class="format-keywords">int</span> result = <span class="format-class">DataEngine</span>.CurrentEngine.ExecuteNoneQuery(Command);<br />
    </p>
</div>
<div class="panel code-panel">
    <h4>QuickDataChanger类</h4>
    使用该类可以不必通过 DbCommandBuilder 来构建插入、更新及删除命令，并且该类提供了将 SpeedDataTable 及 DataTable 数据集批量更新回库的方法。<br />
    命名空间：Wunion.DataAdapter.Kernel<br />
    <p></p>
    <p>
        <strong>QuickDataChanger 更新或插入数据</strong><br />
        <span class="format-class">QuickDataChanger</span> DC = <span class="format-keywords">new</span> <span class="format-class">QuickDataChanger</span>();<br />
        <span class="format-keywords">int</span> result = DC.SaveToDataBase(<span class="format-strVal">"DATA_TABLE"</span>, Data, <span class="format-keywords">false</span>);  
        <span class="format-note">// 最后一个参数为 true 时表示执行更新，为false 时表示执行插入。返回受影响记录数，当返回 -1 时表示执行过程异常。</span><br />
    </p>
    <p>
        <strong>QuickDataChanger 对数据集进行批量更新</strong><br />
        批量更新的机制需要自己定义，以下代码假定在数据集 speedTable 中定义了 STATE 一列来表示每行数据的状态信息，调用 BatchSaveDataTable 方法即可使用这种规则将数据集批量更新至数据库。
        <br />
        <span class="format-class">QuickDataChanger</span> DC = <span class="format-keywords">new</span> <span class="format-class">QuickDataChanger</span>();<br />
        <span class="format-keywords">int</span> result = DC.BatchSaveDataTable(speedTable, (<span class="format-class">SpeedDataRow</span> Row) => { <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-class">DataRowSubmission</span> RowSubmission = null; <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">switch</span> (Row.Field<<span class="format-keywords">int</span>>(<span class="format-strVal">"STATE"</span>))<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">case</span> <span class="format-interface">1</span>:<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission = <span class="format-keywords">new</span> <span class="format-class">DataRowSubmission</span>(<span class="format-interface">QuickDataChangerSubmission</span>.Update);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.NotInsertFields = <span class="format-keywords">new string</span>[] { <span class="format-strVal">"TestId"</span> }; <span class="format-note">// 不更新 TestId 字段</span><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.Conditions.Add(<span class="format-class">td</span>.Field(<span class="format-strVal">"TestId"</span>) == Row.Field<<span class="format-keywords">int</span>>(<span class="format-strVal">"TestId"</span>)); <span class="format-note">// 设置更新条件</span><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">break</span>;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">case</span> <span class="format-interface">2</span>:<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission = <span class="format-keywords">new</span> <span class="format-class">DataRowSubmission</span>(<span class="format-interface">QuickDataChangerSubmission</span>.Insert);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.NotInsertFields = <span class="format-keywords">new string</span>[] { <span class="format-strVal">"TestId"</span> };  <span class="format-note">// 不插入自增长字段</span><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">break</span>;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">case</span> <span class="format-interface">3</span>:<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission = <span class="format-keywords">new</span> <span class="format-class">DataRowSubmission</span>(<span class="format-interface">QuickDataChangerSubmission</span>.Delete);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-note">// 设置删除条件</span><br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.Conditions.Add(<span class="format-class">td</span>.Field(<span class="format-strVal">"TestId"</span>) == Row.Field<<span class="format-keywords">int</span>>(<span class="format-strVal">"TestId"</span>));<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.Conditions.Add(<span class="format-class">exp</span>.And);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission.Conditions.Add(<span class="format-class">td</span>.Field(<span class="format-strVal">"GroupId"</span>) == Row.Field<<span class="format-keywords">int</span>>(<span class="format-strVal">"GroupId"</span>));<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">break</span>;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">default</span>:<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RowSubmission = <span class="format-keywords">new</span> <span class="format-class">DataRowSubmission</span>(<span class="format-interface">QuickDataChangerSubmission</span>.Ignore);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">break</span>;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">return</span> RowSubmission;<br />
        });<br />
    </p>
</div>
<div class="panel code-panel">
    <h4>使用事务处理</h4>
    使用 DataEngine 对象的 BeginTrans() 方法来启动事务，并返回一个 DBTransactionController 事务控制器对象，DBTransactionController 类位于 Wunion.DataAdapter.Kernel.DbInterop 命名空间。<br />
    <p></p>
    <p>
        <strong>在事件中执行命令</strong><br />
        <span class="format-keywords">using</span> (<span class="format-class">DBTransactionController</span> trans = <span class="format-class">DataEngine</span>.CurrentEngine.BeginTrans()) <br />
        { <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.DBA.ExecuteNoneQuery(Command);<br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.Commit();<br />
        } <br />
    </p>
    <p>&nbsp;</p>
    <p>
        <strong>受事务控制的 QuickDataChanger 对象</strong><br />
        <span class="format-keywords">int</span> result = <span class="format-interface">-1</span>;<br />
        <span class="format-keywords">using</span> (<span class="format-class">DBTransactionController</span> trans = <span class="format-class">DataEngine</span>.CurrentEngine.BeginTrans()) <br />
        { <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-class">QuickDataChanger</span> DC = <span class="format-keywords">new</span> <span class="format-class">QuickDataChanger</span>(trans); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DC.Conditions.Add(<span class="format-class">td</span>.Field(<span class="format-strVal">"GroupId"</span>) == GrpId); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DC.Delete(<span class="format-strVal">"DATA_TABLE"</span>); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DC.Conditions.Clear(); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DC.Conditions.Add(<span class="format-class">td</span>.Field(<span class="format-strVal">"Id"</span>) == GrpId); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = DC.Delete(<span class="format-strVal">"GROUP_TABLE"</span>); <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="format-keywords">if</span> (result > <span class="format-interface">0</span>) <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.Commit();  <span class="format-note">// 当分组删除成功时才提交事务</span> <br />
        } <br />
    </p>
</div>